import java.util.Random;

/**
 * @author Sean Barkley, Matthew Metcalf, James Kuglics
 *
 */
public class Client extends Thread{

	//private String name;
	private Banker banker;
	private int nUnits;
	private int nRequests;
	private long minSleepMillis;
	private long maxSleepMillis;
	
	// Test to make random
	private Random rn = new Random();
	private long millis;
	
	public Client(String name, Banker banker, int nUnits, int nRequests, 
			long minSleepMillis, long maxSleepMillis){
		super(name);
		this.banker = banker;
		this.nUnits = nUnits;
		this.nRequests = nRequests;
		this.minSleepMillis = minSleepMillis;
		this.maxSleepMillis = maxSleepMillis;
	}
	
	// TODO
	public void run(){		
		banker.setClaim(nUnits);
		
		for (int i = 0; i < this.nRequests; i++){
			if(banker.remaining() == 0){
				banker.release(nUnits);	
			}else{
				//banker.request(rn.nextInt(nUnits)+1);
				banker.request(nUnits);
			}
			
			try {
				millis = rn.nextInt((int) (this.maxSleepMillis - this.minSleepMillis)) + this.minSleepMillis;
				Thread.sleep(millis);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		banker.release(banker.allocated());
	}
	
}
